# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: ChaosKid42 <christoph.scholz@gmail.com>
# Contributor: Andy Weidenbaum <archbaum@gmail.com>

_name=rugged
# ideally this should match $pkgver, as package() might fail otherwise. thanks, github
_libgit2_pkgver=1.6.3
pkgname=ruby-rugged
pkgver=1.6.2
pkgrel=1
pkgdesc="A Ruby binding to the libgit2 linkable library"
arch=(loong64 x86_64)
url="https://github.com/libgit2/rugged"
license=(MIT)
depends=(
  glibc
  ruby
)
makedepends=(
  libgit2
  ruby-rdoc
)
checkdepends=(
  git
  ruby-minitest
  ruby-rake
  ruby-rake-compiler
)
options=(!emptydirs)
source=(
  $_name-$pkgver.tar.gz::https://github.com/libgit2/$_name/archive/v$pkgver.tar.gz
  libgit2-$_libgit2_pkgver.tar.gz::https://github.com/libgit2/libgit2/archive/v$_libgit2_pkgver.tar.gz
  $pkgname-1.6.2-remove_broken_libgit2_detection.patch
)
sha512sums=('23b9e292dcc2114333ad76d842551a0bea54e3fe487572e41b00480ed4c8b101c9eb9ae0314b44b07257ec448295748f7930bc1fe105bb2ad9c7c24bf813d0f9'
            '5a390030395d5e00217ca3229ff1374f064b41c7c14c332618778af7067c378bca5ffee5c072ad144a71126f7e8cfcd770746b6af8eccf086c8a8b8afa1733f1'
            '955a01271852fcae00750f2bc155e7697e033429fa4f47615d7a611eab5e9296038324d0c486942d878ade118872d1f895f580c08e463b854ab405419673824e')
b2sums=('e17c05bfb27192db70d3704ee6504035699b58532c18442042ae9ae767a5fdd8fb09060d04e15639051459f659c92f804b539a9c07224800f44b0d7fbb51b966'
        'c0a588e962c182c09b91e257760484bc0ebf2ab07fdd89b7b90db1dd6749d7fb65b8e3fcd20e8a46a6381038aeac8759161dc409be36eadc776a55d74290e59c'
        'bf6339a9e4cf26336ea8c693e0ef115b7e4afb1dc56d8b7a5ee3faff7e07eac4b420c67d694c690e78328780730787eec64e816e4c840391500af29a5f4818b0')

prepare() {
  # remove broken and useless libgit2 "version check":
  # https://github.com/libgit2/rugged/issues/698
  patch -Np1 -d $_name-$pkgver -i ../$pkgname-1.6.2-remove_broken_libgit2_detection.patch

  cd $_name-$pkgver
  # we don't do version pinning
  sed -r 's|~>|>=|g' -i $_name.gemspec
  # source tarball doesn't include vendored libgit2 version, which is only
  # needed for test fixtures (resources): https://github.com/libgit2/rugged/issues/801
  mv -v ../libgit2-$_libgit2_pkgver/* vendor/libgit2
  # remove all tests requiring an internet connection
  rm -rv test/online/*
  # remove deprecated `date` element from gemspec. Removing it makes the package reproducible
  sed --in-place '/s\.date/d' "${_name}.gemspec"
}

build() {
  cd $_name-$pkgver
  export CI_BUILD=true
  export RUGGED_USE_SYSTEM_LIBRARIES=true
  export CMAKE_FLAGS=" --use-system-libraries"
  rake compile
  rake gem
}

check(){
  cd $_name-$pkgver
  # ConfigTest#test_read_global_config_file fails, if no gitconfig is set for
  # user...
  # https://github.com/libgit2/rugged/issues/432
  git config --global user.name "Foo Bar"
  rake test --trace --verbose TESTOPTS="--verbose"
}

package() {
  local _gemdir="$(gem env gemdir)"
  depends+=(libgit2.so)

  cd $_name-$pkgver
  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "$pkgdir/$_gemdir" \
    --bindir "$pkgdir/usr/bin" \
    "pkg/$_name-$pkgver.gem"

  # remove unrepreducible files
  rm -frv \
    "$pkgdir/$_gemdir/cache/" \
    "$pkgdir/$_gemdir/gems/$_name-$pkgver/vendor/" \
    "$pkgdir/$_gemdir/doc/$_name-$pkgver/ri/ext/"

  find "$pkgdir/$_gemdir/gems/" \
    -type f \
    \( \
        -iname "*.o" -o \
        -iname "*.c" -o \
        -iname "*.so" -o \
        -iname "*.time" -o \
        -iname "gem.build_complete" -o \
        -iname "Makefile" \
    \) \
    -delete

  find "$pkgdir/$_gemdir/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete

  install -vDm 644 "$pkgdir/$_gemdir/gems/$_name-$pkgver/LICENSE" -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 README.md CHANGELOG.md -t "$pkgdir/usr/share/doc/$pkgname"
}
